﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class JLTVK02XSReport : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        string ABPAUFFKRS;
        string ABPAUFFLFD;
        string ABPAUFFPOS;
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        
        #endregion
        public JLTVK02XSReport()
            : base()
        {
        }
        public JLTVK02XSReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
                      						

        } 
        #region------------------------------Function------------------------
        /*VKPLIEFTERM	VKPPOSTYP	ORDERNO	ITEM	VKPKDKOMM	VKPKOLL	VKPART	VKPFARBE	KDART	KDARTBEZ	KDARTCOLOR	
QTY	VKPMEEH	DELNOTE	ARTNAME	DESCR	VALIDWID	COMPOSITION	PRICS	VKPPREH	VKPWAEHRSL	VKPWUTERM		BRANDNAME		
                    PAYCU	VKPNEUDAT	SCHB	REFERENCES	VKPKKOLL	STRNAME	FINISHING	MQTY	MON	DA*/
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["WASTAMM"];                
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("VKPERL", typeof(System.String));
                dtOutput.Columns.Add("VKPLIEFTERM", typeof(System.String));
                dtOutput.Columns.Add("VKPPOSTYP", typeof(System.String));
                dtOutput.Columns.Add("ORDERNO", typeof(System.String));
                dtOutput.Columns.Add("ITEM", typeof(System.String));
                dtOutput.Columns.Add("VKPKDKOMM", typeof(System.String));
                dtOutput.Columns.Add("VKPKOLL", typeof(System.String));
                dtOutput.Columns.Add("VKPART", typeof(System.String));
                dtOutput.Columns.Add("VKPFARBE", typeof(System.String));
                dtOutput.Columns.Add("KDART", typeof(System.String));
                dtOutput.Columns.Add("KDARTBEZ", typeof(System.String));
                dtOutput.Columns.Add("KDARTCOLOR", typeof(System.String));
                dtOutput.Columns.Add("QTY", typeof(System.String));
                dtOutput.Columns.Add("VKPMEEH", typeof(System.String));
                dtOutput.Columns.Add("ALLQTY", typeof(System.String));
                dtOutput.Columns.Add("DNQTY", typeof(System.String));
                dtOutput.Columns.Add("VKPLVDAT", typeof(System.String));
                dtOutput.Columns.Add("DELNOTE", typeof(System.String));
                dtOutput.Columns.Add("PRDNAME", typeof(System.String));
                dtOutput.Columns.Add("ARTNAME", typeof(System.String));
                dtOutput.Columns.Add("LABELNAME", typeof(System.String));
                dtOutput.Columns.Add("BRANDN", typeof(System.String));
                dtOutput.Columns.Add("DESCR", typeof(System.String));
                dtOutput.Columns.Add("VALIDWID", typeof(System.String));
                dtOutput.Columns.Add("COMPOSITION", typeof(System.String));
                dtOutput.Columns.Add("PRICS", typeof(System.String));
                dtOutput.Columns.Add("VKPPREH", typeof(System.String));
                dtOutput.Columns.Add("VKPWAEHRSL", typeof(System.String));
                dtOutput.Columns.Add("VKPWUTERM", typeof(System.String));
                dtOutput.Columns.Add("BRANDNAME", typeof(System.String));
                dtOutput.Columns.Add("PAYCU", typeof(System.String));
                dtOutput.Columns.Add("VKPNEUDAT", typeof(System.String));
                dtOutput.Columns.Add("SCHB", typeof(System.String));
                dtOutput.Columns.Add("REFERENCES", typeof(System.String));
                dtOutput.Columns.Add("VKPKKOLL", typeof(System.String));
                dtOutput.Columns.Add("STRNAME", typeof(System.String));
                dtOutput.Columns.Add("FINISHING", typeof(System.String));
                dtOutput.Columns.Add("MQTY", typeof(System.String));
                dtOutput.Columns.Add("MON", typeof(System.String));
                dtOutput.Columns.Add("DA", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 3, 1);

                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(3, 13, count + 2, 13);
                exAgent.BatchConvert2Number(3, 15, count + 2, 16);
                exAgent.BatchConvert2Number(3, 26, count + 2, 26);
                exAgent.BatchConvert2Number(3, 38, count + 2, 38);
                exAgent.SetCellValue(1, 1, "订单统计");
                						
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }        
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTReport.xls";
                Standard = "VK02XS";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTReport.xls");
                destfolder = SetFolder(Root, "销售部订单统计");
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string newfilename = @"订单统计" +filedate ;
                
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile("", year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                FillInExcelData(dsSource, 3, 1);
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}